package com.xrz.sxm.aj.https;

import android.content.Context;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xrz.sxm.aj.utils.ToolLoading;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.callback.Callback;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class Http {

	private static Http m_Http;

	public static Http getInstant() {
		if (m_Http == null)
			m_Http = new Http();
		return m_Http;
	}

	private Http() {
	}

	public void sendRequest(final Context context, Map<String, String> map, final HttpListener listener) {
		sendRequest(context, true, map, listener);
	}

	/**
	 * 只需显示返回结果得请求
	 * 
	 * @param ctx
	 * @param map
	 */
	public void sendRequest(final Context ctx, Map<String, String> map) {
		sendRequest(ctx, true, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				Toast.makeText(ctx, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}

	public String sendRequestSync(Map<String, String> map) {
//		PostFormBuilder builder = OkHttpUtils.post().url(HttpConst.BASEURL);
		GetBuilder builder = OkHttpUtils.get().url(HttpConst.BASEURL);
		if (map != null)
			for (Map.Entry<String, String> entry : map.entrySet()) {
				builder.addParams(entry.getKey(), entry.getValue());
			}
		try {
			Response response = builder.build().execute();
			ResponseBody body = response.body();
			return body.string();
		} catch (IOException e) {
		}
		return null;
	}
	
	public String sendRequestSync(String url,Map<String, String> map) {
//		PostFormBuilder builder = OkHttpUtils.post().url(url);
		GetBuilder builder = OkHttpUtils.get().url(url);
		if (map != null)
			for (Map.Entry<String, String> entry : map.entrySet()) {
				builder.addParams(entry.getKey(), entry.getValue());
			}
		try {
			Response response = builder.build().execute();
			ResponseBody body = response.body();
			return body.string();
		} catch (IOException e) {
		}
		return null;
	}

	public void test(final Context context, final boolean isshowDialog, Map<String, String> map,
			final HttpListener listener) {
		if (isshowDialog)
			ToolLoading.ShowLoading(context);
//		PostFormBuilder builder = OkHttpUtils.post().url(HttpConst.BASEURL);
		GetBuilder builder = OkHttpUtils.get().url(HttpConst.BASEURL);
		if (map != null)
			for (Map.Entry<String, String> entry : map.entrySet()) {
				builder.addParams(entry.getKey(), entry.getValue());
			}
		builder.build().execute(new Callback<String>() {

			/**
			 * Thread Pool Thread
			 *
			 * @param response
			 * @param id
			 */
			@Override
			public String parseNetworkResponse(Response response, int id) throws Exception {
				String result = response.body().string();
				return result;
			}

			@Override
			public void onError(Call call, Exception e, int id) {

			}

			@Override
			public void onResponse(String response, int id) {
				listener.onHttpSuccess(response);
			}

		});
	}

	public void sendRequest(final Context context, final boolean isshowDialog, final String url,
			Map<String, String> map, final HttpListener listener) {
		if (isshowDialog)
			ToolLoading.ShowLoading(context);
//		PostFormBuilder builder = OkHttpUtils.post().url(url);
		GetBuilder builder = OkHttpUtils.get().url(url);
		if (map != null)
			for (Map.Entry<String, String> entry : map.entrySet()) {
				builder.addParams(entry.getKey(), entry.getValue());
			}
		builder.build().execute(new Callback<String>() {

			/**
			 * Thread Pool Thread
			 *
			 * @param response
			 * @param id
			 */
			@Override
			public String parseNetworkResponse(Response response, int id) throws Exception {
				String result = response.body().string();
				return result;
			}

			@Override
			public void onError(Call call, Exception e, int id) {
				if (isshowDialog)
					ToolLoading.closeLoading(context);
				Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
				listener.onHttpError(e.toString());
			}

			@Override
			public void onResponse(String result, int id) {
				if (isshowDialog)
					ToolLoading.closeLoading(context);
					listener.onHttpSuccess(result);
			}

		});
	}

	public void sendRequest(final Context context, final boolean isshowDialog, Map<String, String> map,
			final HttpListener listener) {
		sendRequest(context, isshowDialog, HttpConst.BASEURL, map, listener);

	}


}
